//
//  UPLoanStateView.m
//  Uang Pasti
//
//  Created by 李永彬 on 2025/8/28.
//

#import "UPLoanStateView.h"

@interface UPLoanStateView ()
@property (nonatomic, assign) UPLoanStateViewType loanStateViewType;
@property (nonatomic, copy) UPLoanStateViewRetryBlock retryBlock;
@end

@implementation UPLoanStateView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = [UIColor clearColor];
    self.hidden = YES;
    
    // 图标
    self.iconImageView = [[UIImageView alloc] init];
    self.iconImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:self.iconImageView];
    
    // 标题
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.textColor = [UIColor colorWithHexString:@"#000000"];
    self.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightSemibold];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.numberOfLines = 0;
    [self addSubview:self.titleLabel];
    
    // 重试按钮
    self.retryButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.retryButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.retryButton.backgroundColor = [UIColor colorWithHexString:@"#000000"];
    self.retryButton.layer.cornerRadius = 22;
    self.retryButton.clipsToBounds = YES;
    [self.retryButton setContentEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
    [self.retryButton addTarget:self action:@selector(retryButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.retryButton];
    
    // 约束
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.centerX.equalTo(self);
        make.width.mas_equalTo(151);
        make.height.mas_equalTo(135);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconImageView.mas_bottom).offset(20);
        make.left.right.equalTo(self);
    }];
    
    [self.retryButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(40);
        make.centerX.equalTo(self);
        make.height.mas_equalTo(44);
        make.bottom.equalTo(self);
    }];
}

- (void)updateWithType:(UPLoanStateViewType)type
                  icon:(NSString *)iconName
                 title:(NSString *)title
           buttonTitle:(NSString *)buttonTitle
            retryBlock:(UPLoanStateViewRetryBlock)retryBlock {
    
    self.loanStateViewType = type;
    self.iconImageView.image = [UIImage imageNamed:iconName];
    self.titleLabel.text = title;
    [self.retryButton setTitle:buttonTitle forState:UIControlStateNormal];
    self.retryBlock = retryBlock;
}

- (void)retryButtonTapped {
    if (self.retryBlock) {
        self.retryBlock();
    }
}

@end
